package com.sxfq.loansupermarket.backsystem.modular.theTest.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.util.CommUtils;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.system.dao.UserMgrDao;
import com.sxfq.loansupermarket.backsystem.modular.theTest.dao.TestingDao;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.SearchTestingInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.TestQuestionsInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.TheTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.UserForTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.ITestingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TestingServiceImpl implements ITestingService {

    @Autowired
    private TestingDao testingDao;
    @Autowired
    UserMgrDao userMgrDao;
    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<TestQuestionsInfo> selectPageListByQueryGetMap(Page<TestQuestionsInfo> page, SearchTestingInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        params.put("theTestId", searchInfo.getTheTestId());
        params.put("userId", searchInfo.getUserId());
        return testingDao.getQueryPage(page, params);
    }

    public List<UserForTestInfo> selectPageListgetScore(String theTestId, String userId) {
        return testingDao.getScorePage(theTestId, userId);
    }

    public void changeScore(int userId, Double testScores) {
        userMgrDao.changeScore(userId, testScores);
    }

    /*
    *生成考试题
    */
    @Transactional
    public TheTestInfo testGenerate(String theTestId) {
        String sql = "SELECT * from the_test_info where id=#{id}";
        TheTestInfo theTestInfo = sqlMapper.selectOne(sql, theTestId, TheTestInfo.class);
//        String testGenerateSql = "SELECT * from test_questions_info ORDER BY RAND() LIMIT  " + theTestInfo.getNumberOfQuestion() + "";
//        String testGenerateSql = "(select * from (select * from test_questions_info a where a.title_type='口内') b  ORDER BY RAND() LIMIT 20) " +
//                "union all (select * from (select * from test_questions_info a where a.title_type='口外') b  ORDER BY RAND() LIMIT 20) " +
//                "union all (select * from (select * from test_questions_info a where a.title_type='口修') b  ORDER BY RAND() LIMIT 25) " +
//                "union all (select * from (select * from test_questions_info a where a.title_type='口解') b  ORDER BY RAND() LIMIT 20) " +
//                "union all (select * from (select * from test_questions_info a where a.title_type='咿呀文化') b  ORDER BY RAND() LIMIT 5) " +
//                "union all (select * from (select * from test_questions_info a where a.title_type='其他') b  ORDER BY RAND() LIMIT 10)";
//        String testGenerateSql ="select * from test_questions_info";
        String testGenerateSql = "select * from test_questions_info a where a.title_type='咨询师' ORDER BY RAND() LIMIT 50";
        List<TestQuestionsInfo> testQuestionsInfoList = sqlMapper.selectList(testGenerateSql, theTestInfo.getNumberOfQuestion().toString(), TestQuestionsInfo.class);
        String insertUserForTestSql = "insert into user_for_test_info (id,test_status,test_number,test_questions_id,user_id,the_test_id,start_time,end_time) values";
        int i = 0;
        for (TestQuestionsInfo testQuestionsInfo : testQuestionsInfoList) {
            i++;
            insertUserForTestSql += "('" + CommUtils.getUUID() + "'," + 1 + "," + i + ",'" + testQuestionsInfo.getId() + "'," + ShiroKit.getUser().getId() + ", '" + theTestId +
                    "', '" + DateTimeKit.now() + "', " + "date_add('" + DateTimeKit.now() + "', interval " + theTestInfo.getTimeLength() + " minute)" + ")";
            if (i != testQuestionsInfoList.size()) {
                insertUserForTestSql += ",";
            }
        }
        sqlMapper.insert(insertUserForTestSql);
        return theTestInfo;
    }

    @Transactional
    public UserForTestInfo selectOneById(String id) {
        String sql = "select * from user_for_test_info where id= '" + id + "'";
        UserForTestInfo info = sqlMapper.selectOne(sql, UserForTestInfo.class);
        return info;
    }

    @Transactional
    public List<TestQuestionsInfo> getList(String theTestId, Integer userId) {
        String sql = "     SELECT a.id,b.options_a,b.options_b, b.options_d,b.options_c,b.options_e,b.options_f, " +
                "        a.user_answer,a.test_questions_id,a.test_number,b.test_title,b.correct_answer,a.current_page " +
                "         from user_for_test_info a left join test_questions_info b on a.test_questions_id=b.id " +
                "        where 1=1  and the_test_id='" + theTestId + "' and user_id=" + userId +
                "        ORDER BY a.test_number ASC ";
        List<TestQuestionsInfo> testQuestionsInfoList = sqlMapper.selectList(sql, TestQuestionsInfo.class);
        return testQuestionsInfoList;
    }

    @Transactional
    public List<TestQuestionsInfo> getErrorList(Integer userId, String titleType) {
        String sql = " select (@i:=@i+1) testNumber, t1.id,t2.options_a,t2.options_b, t2.options_d,t2.options_c,t2.options_e,t2.options_f, t2.title_type, " +
                "  t2.test_title,t2.title_type,t1.current_page,t2.correct_answer " +
                "  from error_questions_info t1 left join test_questions_info t2 on t1.test_questions_id=t2.id , (SELECT @i:=0) as i " +
                " where t1.user_id=" + userId + "  and t2.title_type='" + titleType + "'   group by t2.id  order by     testNumber   ";
        List<TestQuestionsInfo> testQuestionsInfoList = sqlMapper.selectList(sql, TestQuestionsInfo.class);
        return testQuestionsInfoList;
    }

    @Transactional
    public List<TestQuestionsInfo> getReciteList(String titleType) {
        String sql = "     SELECT (@i:=@i+1) testNumber, b.id,b.options_a,b.options_b, b.options_d,b.options_c,b.options_e,b.options_f, " +
                "      b.test_title,b.correct_answer, b.title_type " +
                "     from   test_questions_info b  , (SELECT @i:=0) as i " +
                "    where 1=1  and b.title_type='" + titleType + "' ";
        List<TestQuestionsInfo> testQuestionsInfoList = sqlMapper.selectList(sql, TestQuestionsInfo.class);
        return testQuestionsInfoList;
    }

    @Transactional
    public UserForTestInfo getReciteCurrentPage(Integer userId) {
        String sql = "select * from user where id=" + userId + ";";
        UserForTestInfo userForTestInfo = sqlMapper.selectOne(sql, UserForTestInfo.class);
        return userForTestInfo;
    }

}
